package com.easy.dn.ui.mainFragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easy.common.base.BaseFragment;
import com.easy.common.commonwidget.RippleView;
import com.easy.dn.bean.JsonDataBean;
import com.easy.dn.ui.publish.adapter.EducationAdapter;
import com.easy.dn.ui.publish.adapter.IdentityAdapter;
import com.easy.dn.ui.publish.adapter.LanguageAdapter;
import com.easy.dn.ui.publish.adapter.PositionNatureAdapter;
import com.easy.dn.ui.publish.adapter.PositionSuitAdapter;
import com.easy.dn.utils.CommonDataUtil;
import com.qmuiteam.qmui.util.QMUIDirection;
import com.qmuiteam.qmui.util.QMUIViewHelper;

import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import dn.easy.com.easyto.R;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * ░░░░░░░░░░░░░░░░░░░░░░░░▄░░
 * ░░░░░░░░░▐█░░░░░░░░░░░▄▀▒▌░
 * ░░░░░░░░▐▀▒█░░░░░░░░▄▀▒▒▒▐
 * ░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐
 * ░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐
 * ░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌
 * ░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒
 * ░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐
 * ░▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄
 * ░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒
 * ▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒
 * 单身狗就这样默默地看着你，一句话也不说。
 * 发布
 *
 * @author DN
 * @data 2017/9/12
 **/

public class PublishFragment extends BaseFragment implements RippleView.OnRippleCompleteListener {
    @Bind(R.id.publishBtn)
    RippleView mPublishBtn;
    @Bind(R.id.perviewBtn)
    RippleView mPrviewBtn;
    @Bind(R.id.linear)
    LinearLayout mSetp1Linear;
    @Bind(R.id.linear_setp1)
    LinearLayout mSetp1Liear;
    @Bind(R.id.topBtn)
    RippleView mTopBtn;
    @Bind(R.id.nextBtn)
    RippleView mNextBtn;
    @Bind(R.id.tv_finish)
    TextView finishTxt;
    private String[] mIdentityArr;
    private IdentityAdapter mIdentityAdapter;
    private List<JsonDataBean> mIdentityList;
    private List<JsonDataBean> mPostionNatureList;
    private List<JsonDataBean> mPostionSuitList;
    private List<JsonDataBean> mEducationList;
    private List<JsonDataBean> mLanguageList;
    private PositionNatureAdapter mPositionNatureAdapter;
    private PositionSuitAdapter mPositionSuitAdapter;
    private EducationAdapter mEducationAdapter;
    private LanguageAdapter mLanguageAdapter;
    private double mStartTime;
    private LinearLayout mLinearLayout;
    private int mSelectNumber;
    private RecyclerView mIdentityRecyclerView;
    private RecyclerView mPsitionNatureRecyclerView;
    private RecyclerView mPositionRecyclerView;
    private RecyclerView mEducationRecyclerView;
    private RecyclerView mLanguagenRecyclerView;

    @Override
    protected int getLayoutResource() {
        mStartTime = System.currentTimeMillis();
        return R.layout.fra_publish;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected void initView() {
        // 延時操作符 防止點擊進入發佈界面 加載時間過長 體驗不好
        Observable.timer(200, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(Long aLong) {
                        initSetp2();
//                        initSetp3();
                    }
                });
        mSelectNumber = 0;
        mTopBtn.setVisibility(View.GONE);
        mPublishBtn.setVisibility(View.GONE);
        mPrviewBtn.setVisibility(View.GONE);
        mNextBtn.setOnRippleCompleteListener(this);
        mTopBtn.setOnRippleCompleteListener(this);
        mPublishBtn.setOnRippleCompleteListener(this);
        mPrviewBtn.setOnRippleCompleteListener(this);

        double endTime = System.currentTimeMillis(); //获取结束时间
        Log.i("Dnlog", "----------------消耗时间：" + (endTime - mStartTime) / 1000 + "s");
    }

    //2 界面
    private void initSetp2() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.include_publish_setp2, null);
        mLinearLayout = (LinearLayout) view.findViewById(R.id.linear_setp2);
        mLinearLayout.setVisibility(View.GONE);
        mIdentityRecyclerView = (RecyclerView) view.findViewById(R.id.identityRecyclerView);
        mPsitionNatureRecyclerView = (RecyclerView) view.findViewById(R.id.psitionNatureRecyclerView);
        mPositionRecyclerView = (RecyclerView) view.findViewById(R.id.positionRecyclerView);
        mEducationRecyclerView = (RecyclerView) view.findViewById(R.id.educationRecyclerView);
        mLanguagenRecyclerView = (RecyclerView) view.findViewById(R.id.languagenRecyclerView);
        mSetp1Linear.addView(view);
        //获取开始时间
        //初始化身份要求
        //需要区分开中英文 目前没搞
        mIdentityList = CommonDataUtil.getJsonList("position_data.json", "zhIdentityData");
        mIdentityRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        mIdentityRecyclerView.setNestedScrollingEnabled(false);
        mIdentityAdapter = new IdentityAdapter(R.layout.item_common_checked, mIdentityList);
        mIdentityRecyclerView.setAdapter(mIdentityAdapter);
        //职位性质
        mPostionNatureList = CommonDataUtil.getJsonList("position_data.json", "zhPositionNatureData");
        mPsitionNatureRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        mPsitionNatureRecyclerView.setNestedScrollingEnabled(false);
        mPositionNatureAdapter = new PositionNatureAdapter(R.layout.item_common_checked, mPostionNatureList);
        mPsitionNatureRecyclerView.setAdapter(mPositionNatureAdapter);
        //职位适合
        mPostionSuitList = CommonDataUtil.getJsonList("position_data.json", "zhPositionSuitData");
        mPositionRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        mPositionRecyclerView.setNestedScrollingEnabled(false);
        mPositionSuitAdapter = new PositionSuitAdapter(R.layout.item_common_checked, mPostionSuitList);
        mPositionRecyclerView.setAdapter(mPositionSuitAdapter);
        //学历要求
        mEducationList = CommonDataUtil.getJsonList("position_data.json", "zhEducationData");
        mEducationRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        mEducationRecyclerView.setNestedScrollingEnabled(false);
        mEducationAdapter = new EducationAdapter(R.layout.item_common_checked, mEducationList);
        mEducationRecyclerView.setAdapter(mEducationAdapter);
        //语言要求
        mLanguageList = CommonDataUtil.getJsonList("position_data.json", "zhLanguageData");
        mLanguagenRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        mLanguagenRecyclerView.setNestedScrollingEnabled(false);
        mLanguageAdapter = new LanguageAdapter(R.layout.item_common_checked, mLanguageList);
        mLanguagenRecyclerView.setAdapter(mLanguageAdapter);
    }

    @Override
    public void onComplete(RippleView rippleView) {
        switch (rippleView.getId()) {
            case R.id.topBtn:
                if (mSelectNumber == 1) {
                    mSelectNumber = 0;
                    if (mPublishBtn != null) mPublishBtn.setVisibility(View.GONE);
                    if (mPrviewBtn != null) mPrviewBtn.setVisibility(View.GONE);
                    mTopBtn.setVisibility(View.GONE);
                    mNextBtn.setVisibility(View.VISIBLE);
                    QMUIViewHelper.slideOut(mLinearLayout, 50, null, true, QMUIDirection.BOTTOM_TO_TOP);
                    QMUIViewHelper.slideIn(mSetp1Liear, 300, null, true, QMUIDirection.TOP_TO_BOTTOM);
                }
                break;
            case R.id.nextBtn:
                if (mSelectNumber == 0) {
                    mSelectNumber = 1;
                    if (mPublishBtn != null)  mPublishBtn.setVisibility(View.VISIBLE);
                    if (mPrviewBtn != null)  mPrviewBtn.setVisibility(View.VISIBLE);
                    mTopBtn.setVisibility(View.VISIBLE);
                    mNextBtn.setVisibility(View.GONE);
                    QMUIViewHelper.slideOut(mSetp1Liear, 50, null, true, QMUIDirection.BOTTOM_TO_TOP);
                    QMUIViewHelper.slideIn(mLinearLayout, 300, null, true, QMUIDirection.BOTTOM_TO_TOP);
                }
                break;
        }
    }
}
